﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Linq.Expressions;
using System.Configuration;

namespace Microhard.Framework.MVCHelper
{
    public static class HtmlExtension
    {
        public static string FieldIdFor<T, TResult>(this HtmlHelper<T> html, Expression<Func<T, TResult>> expression)
        {
            var id = html.ViewData.TemplateInfo.GetFullHtmlFieldId(ExpressionHelper.GetExpressionText(expression));
            // because "[" and "]" aren't replaced with "_" in GetFullHtmlFieldId
            return id.Replace('[', '_').Replace(']', '_');
        }

        public static string GetCurrentUserName(this HtmlHelper html)
        { 
            if(html.ViewContext.HttpContext.User.Identity.IsAuthenticated)
            {
                return html.ViewContext.HttpContext.User.Identity.Name;
            }
            return "无法获取";
        }

        public static object GetCurrentUserInfoFromSession(this HtmlHelper value)
        {
            return value.ViewContext.HttpContext.Session["userInfo"];
        }

        public static object GetCurrentUserInfoFromSessionByCtr(this Controller value)
        {
            return null;
        }

        public static int GetGridPageSize(this HtmlHelper html)
        {
            return int.Parse(ConfigurationManager.AppSettings["GridPageSize"]);
        }
    }




}
